package hms.RoomManagementModule;

import hms.rooms.RoomTable;
import hms.rooms.RoomTypeTable;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ConfigureRoomsPanel extends JPanel {
	protected JTextField txt_label, txt_floor;
	
	protected JButton btn_addRoom, btn_removeRoom, btn_editType, btn_newType;
	
	protected JComboBox combo_roomTypes;
	
	protected DefaultListModel listModel;
	
	protected JTextArea txt_additionalNotes;
	
	protected JLabel lbl_roomLabel, lbl_floor, lbl_additionalNotes, lbl_occupiedStatus;
	
	protected DefaultComboBoxModel model;
	
	protected JList list_rooms;
	
	private ConfigureRoomsView view;
	private JButton btn_updateRoom;

	/**
	 * Enable the Update button
	 */
	public void enableUpdateButton(){
		btn_updateRoom.setEnabled(true);
	}
	
	/**
	 * Disable the update button
	 */
	public void disableUpdateButton(){
		btn_updateRoom.setEnabled(false);
	}
	
	/**
	 * Disable the add room button
	 */
	public void disableAddRoomButton(){
		btn_addRoom.setEnabled(false);
	}
	
	/**
	 * Enable the add room button
	 */
	public void enableAddRoomButton(){
		btn_addRoom.setEnabled(true);
	}
	
	/**
	 * Enable the Remove button
	 */
	public void enableRemoveButton(){
		btn_removeRoom.setEnabled(true);
	}
	
	/**
	 * Disable the Remove button
	 */
	public void disableRemoveButton(){
		btn_removeRoom.setEnabled(false);
	}
	
	/**
	 * Fill out the components with information from the database
	 */
	public void fillOutComponents(){
		RoomTable rooms = new RoomTable();
		listModel.clear();
		listModel.addElement("-- Add New Room --");
		for(int i = 0; i < rooms.getNumberOfRooms(); i++){
			listModel.addElement(rooms.getElementAt(i).toString());
		}
		
		RoomTypeTable roomTypes = new RoomTypeTable();
		model = new DefaultComboBoxModel();
		model.removeAllElements();
		for(int i = 0; i < roomTypes.getNumberOfRoomTypes(); i++){
			model.addElement(roomTypes.getElementAt(i).toString());
		}
		combo_roomTypes.setModel(model);
	}
	
	/**
	 * Create the panel.
	 */
	public ConfigureRoomsPanel(ConfigureRoomsView i_view) {

		view = i_view;
		
		listModel = new DefaultListModel();
		
		JLabel lbl_roomsList = new JLabel("Rooms:");
		lbl_roomsList.setFont(new Font("SansSerif", Font.PLAIN, 20));
		
		JPanel panel = new JPanel();

		panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		JLabel lbl_roomInformation = new JLabel("Room Information:");
		lbl_roomInformation.setFont(new Font("SansSerif", Font.PLAIN, 20));
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(16)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 891, Short.MAX_VALUE)
						.addComponent(lbl_roomInformation, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lbl_roomsList, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 356, GroupLayout.PREFERRED_SIZE))
					.addGap(17))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lbl_roomsList, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_roomInformation, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 607, Short.MAX_VALUE)
							.addGap(64))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
							.addGap(118))))
		);
		
		 list_rooms = new JList(listModel);
		 list_rooms.addListSelectionListener(view);
			
		scrollPane.setViewportView(list_rooms);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{48, 226, 106, 61, 13, 174, 180, 0};
		gbl_panel.rowHeights = new int[]{42, 26, 26, 29, 47, 148, 160, 47, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		lbl_roomLabel = new JLabel("Label:");
		lbl_roomLabel.setFont(new Font("SansSerif", Font.PLAIN, 26));
		GridBagConstraints gbc_lbl_roomLabel = new GridBagConstraints();
		gbc_lbl_roomLabel.fill = GridBagConstraints.BOTH;
		gbc_lbl_roomLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_roomLabel.gridx = 1;
		gbc_lbl_roomLabel.gridy = 1;
		panel.add(lbl_roomLabel, gbc_lbl_roomLabel);
		
		txt_label = new JTextField();
		txt_label.setFont(new Font("Roboto Cn", Font.PLAIN, 22));
		txt_label.setColumns(10);
		GridBagConstraints gbc_txt_label = new GridBagConstraints();
		gbc_txt_label.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_label.gridwidth = 3;
		gbc_txt_label.insets = new Insets(0, 0, 5, 5);
		gbc_txt_label.gridx = 2;
		gbc_txt_label.gridy = 1;
		panel.add(txt_label, gbc_txt_label);
		
		lbl_floor =  new JLabel("Floor:");
		lbl_floor.setFont(new Font("SansSerif", Font.PLAIN, 26));
		GridBagConstraints gbc_lbl_floor = new GridBagConstraints();
		gbc_lbl_floor.fill = GridBagConstraints.BOTH;
		gbc_lbl_floor.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_floor.gridx = 1;
		gbc_lbl_floor.gridy = 2;
		panel.add(lbl_floor, gbc_lbl_floor);
		
		txt_floor = new JTextField();
		txt_floor.setFont(new Font("Roboto Cn", Font.PLAIN, 22));
		txt_floor.setColumns(10);
		GridBagConstraints gbc_txt_floor = new GridBagConstraints();
		gbc_txt_floor.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_floor.gridwidth = 3;
		gbc_txt_floor.insets = new Insets(0, 0, 5, 5);
		gbc_txt_floor.gridx = 2;
		gbc_txt_floor.gridy = 2;
		panel.add(txt_floor, gbc_txt_floor);
		
		JLabel lbl_roomType = new JLabel("Room Type:");
		lbl_roomType.setFont(new Font("SansSerif", Font.PLAIN, 26));
		GridBagConstraints gbc_lbl_roomType = new GridBagConstraints();
		gbc_lbl_roomType.anchor = GridBagConstraints.EAST;
		gbc_lbl_roomType.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_roomType.gridx = 5;
		gbc_lbl_roomType.gridy = 2;
		panel.add(lbl_roomType, gbc_lbl_roomType);
		
		combo_roomTypes = new JComboBox();
		combo_roomTypes.setFont(new Font("Roboto Cn", Font.PLAIN, 26));
		GridBagConstraints gbc_combo_roomTypes = new GridBagConstraints();
		gbc_combo_roomTypes.anchor = GridBagConstraints.WEST;
		gbc_combo_roomTypes.insets = new Insets(0, 0, 5, 0);
		gbc_combo_roomTypes.gridx = 6;
		gbc_combo_roomTypes.gridy = 2;
		panel.add(combo_roomTypes, gbc_combo_roomTypes);
		
		lbl_additionalNotes = new JLabel("Additional Notes:");
		lbl_additionalNotes.setFont(new Font("SansSerif", Font.PLAIN, 20));
		GridBagConstraints gbc_lbl_additionalNotes = new GridBagConstraints();
		gbc_lbl_additionalNotes.anchor = GridBagConstraints.NORTHWEST;
		gbc_lbl_additionalNotes.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_additionalNotes.gridx = 1;
		gbc_lbl_additionalNotes.gridy = 4;
		panel.add(lbl_additionalNotes, gbc_lbl_additionalNotes);
		
		btn_editType = new JButton("Edit Type");
		btn_editType.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_editType.setActionCommand("btn_editType");
		btn_editType.addActionListener(view);
		GridBagConstraints gbc_btn_editType = new GridBagConstraints();
		gbc_btn_editType.fill = GridBagConstraints.BOTH;
		gbc_btn_editType.insets = new Insets(0, 0, 5, 5);
		gbc_btn_editType.gridx = 5;
		gbc_btn_editType.gridy = 4;
		panel.add(btn_editType, gbc_btn_editType);
		
		btn_newType = new JButton("New Type");
		btn_newType.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_newType.setActionCommand("btn_newType");
		btn_newType.addActionListener(view);
		GridBagConstraints gbc_btn_newType = new GridBagConstraints();
		gbc_btn_newType.fill = GridBagConstraints.BOTH;
		gbc_btn_newType.insets = new Insets(0, 0, 5, 0);
		gbc_btn_newType.gridx = 6;
		gbc_btn_newType.gridy = 4;
		panel.add(btn_newType, gbc_btn_newType);
		
		txt_additionalNotes = new JTextArea();
		txt_additionalNotes.setFont(new Font("Roboto Cn", Font.PLAIN, 22));
		txt_additionalNotes.setMaximumSize(txt_additionalNotes.getSize());
		txt_additionalNotes.setLineWrap(true);
		txt_additionalNotes.setRows(25);
		//txt_additionalNotes.setWrapStyleWord(true);
		
		GridBagConstraints gbc_txt_additionalNotes = new GridBagConstraints();
		gbc_txt_additionalNotes.gridheight = 2;
		gbc_txt_additionalNotes.fill = GridBagConstraints.BOTH;
		gbc_txt_additionalNotes.insets = new Insets(0, 0, 5, 5);
		gbc_txt_additionalNotes.gridwidth = 4;
		gbc_txt_additionalNotes.gridx = 1;
		gbc_txt_additionalNotes.gridy = 5;
		panel.add(txt_additionalNotes, gbc_txt_additionalNotes);
		
		btn_updateRoom = new JButton("Update Room");
		btn_updateRoom.setEnabled(false);
		btn_updateRoom.setVerticalAlignment(SwingConstants.BOTTOM);
		btn_updateRoom.setFont(new Font("Roboto Cn", Font.PLAIN, 20));
		btn_updateRoom.setActionCommand("btn_updateRoom");
		btn_updateRoom.addActionListener(view);
		GridBagConstraints gbc_btn_updateRoom = new GridBagConstraints();
		gbc_btn_updateRoom.fill = GridBagConstraints.HORIZONTAL;
		gbc_btn_updateRoom.anchor = GridBagConstraints.SOUTH;
		gbc_btn_updateRoom.insets = new Insets(0, 0, 5, 5);
		gbc_btn_updateRoom.gridx = 5;
		gbc_btn_updateRoom.gridy = 5;
		panel.add(btn_updateRoom, gbc_btn_updateRoom);
		
		lbl_occupiedStatus = new JLabel("");
		lbl_occupiedStatus.setFont(new Font("Roboto Cn", Font.PLAIN, 15));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.SOUTH;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 6;
		gbc_lblNewLabel.gridy = 5;
		panel.add(lbl_occupiedStatus, gbc_lblNewLabel);
		
		btn_addRoom = new JButton("Add Room");
		GridBagConstraints gbc_btn_addRoom = new GridBagConstraints();
		gbc_btn_addRoom.fill = GridBagConstraints.BOTH;
		gbc_btn_addRoom.insets = new Insets(0, 0, 5, 5);
		gbc_btn_addRoom.gridx = 5;
		gbc_btn_addRoom.gridy = 6;
		panel.add(btn_addRoom, gbc_btn_addRoom);
		btn_addRoom.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_addRoom.setActionCommand("btn_addRoom");
		btn_addRoom.addActionListener(view);
		
		btn_removeRoom = new JButton("Remove Room");
		btn_removeRoom.setEnabled(false);
		GridBagConstraints gbc_btn_removeRoom = new GridBagConstraints();
		gbc_btn_removeRoom.insets = new Insets(0, 0, 5, 0);
		gbc_btn_removeRoom.fill = GridBagConstraints.BOTH;
		gbc_btn_removeRoom.gridx = 6;
		gbc_btn_removeRoom.gridy = 6;
		panel.add(btn_removeRoom, gbc_btn_removeRoom);
		btn_removeRoom.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_removeRoom.setActionCommand("btn_removeRoom");
		btn_removeRoom.addActionListener(view);
		setLayout(groupLayout);

		
		fillOutComponents();
	}
}
